package com.aptemo.webetek.client.widgets.userinfo;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.mvp.BasePresenter;
import com.aptemo.webetek.shared.LoginBean;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;

public class UserInfoPresenter implements BasePresenter
{

	public interface Display extends BaseDisplay
	{
		HasText getRole();

		HasText getUserEmail();

		HasText getSigninLink();

		void setLoginTarget(String loginUrl);

		String translate(String key);

	}

	private Display		mdisplay;

	private LoginBean	loginInfo;

	public UserInfoPresenter(LoginBean user, HandlerManager eventbus, WebetekUserServiceAsync rpc, Display display)
	{
		mdisplay = display;
		this.loginInfo = user;
	}

	private void bindDisplay()
	{

	}

	protected void refresh()
	{
		if (loginInfo != null && true == loginInfo.isLoggedIn())
		{
			String email = loginInfo.getEmail();
			mdisplay.getUserEmail().setText(email);
			mdisplay.getSigninLink().setText(mdisplay.translate("signout"));
			mdisplay.setLoginTarget(loginInfo.getLogoutUrl());
		}
		else
		{
			mdisplay.getUserEmail().setText("<" + mdisplay.translate("guest") + ">");
			mdisplay.getSigninLink().setText(mdisplay.translate("signin"));
			mdisplay.setLoginTarget(loginInfo.getLoginUrl());
		}
	}

	@Override
	public void go(HasWidgets container)
	{
		bindDisplay();
		container.clear();
		container.add(mdisplay.getDisplay());

		refresh();
	}

}
